﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Slwy.Core.DomainService.IService;
using Slwy.Core.Entities;
using Slwy.Core.Repository.EF;
using Slwy.MySql.Contracts;
using SqlSugar;

namespace Slwy.Core.DomainService
{
    public class CarrierService : ICarrierService
    {
        private readonly IMySqlConnection _mySqlConnection;

        public CarrierService(IMySqlConnection mySqlConnection)
        {
            this._mySqlConnection = mySqlConnection;
        }

        public bool AddCarrierByEF(IList<CarrierInfo> entities)
        {
            using (var context = new CarrierContext(this._mySqlConnection))
            {
                try
                {
                    // context.AddRange(entities);
                    context.Add(entities.First());
                    context.SaveChanges();
                }
                catch (Exception e)
                {
                    return false;
                }

                return true;
            }
        }

        public bool AddCarrierBySqlSugar(IList<CarrierInfo> entities)
        {
            var connectionString = this._mySqlConnection.GetWriteConnectionString();
            using (var client = new SqlSugarClient(new ConnectionConfig
            {
                ConnectionString = connectionString, //必填
                DbType = DbType.MySql, //必填
                IsAutoCloseConnection = true //默认false
            }))
            {
                try
                {
                    return client.Insertable(entities.First()).ExecuteCommand() > 0;
                    //return client.Insertable(entities.ToArray()).ExecuteCommand() > 0;
                }
                catch (Exception e)
                {
                    return false;
                }
            }
        }

        public bool UpdateCarrierByEF()
        {
            using (var context = new CarrierContext(this._mySqlConnection))
            {
                var carrierInfos = context.CarrierInfo.Where(m => m.KeyId > 564);
                carrierInfos.ForEachAsync(m => m.AddUser = "压力测试2");
                context.CarrierInfo.UpdateRange(carrierInfos);
                context.SaveChanges();
            }

            return true;
        }

        public bool UpdateCarrierBySqlSugar()
        {
            var connectionString = this._mySqlConnection.GetWriteConnectionString();
            using (var client = new SqlSugarClient(new ConnectionConfig
            {
                ConnectionString = connectionString, //必填
                DbType = DbType.MySql, //必填
                IsAutoCloseConnection = true //默认false
            }))
            {
                try
                {
                    return client.Updateable<CarrierInfo>(new CarrierInfo{AddUser = "压力测试3"}).Where(m => m.KeyId > 564).UpdateColumns(m => m.AddUser)
                        .ExecuteCommand() > 0;
                }
                catch (Exception e)
                {
                    return false;
                }
            }
        }
    }
}
